package com.example.adminbase.mybatisgenerator;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.JavaElement;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;

public class MybatisGeneratorUtil {
    public static Field buildDeletableField(String name, FullyQualifiedJavaType type) {
        Field field = new Field(name, type);
        addDeletableDocLine(field);
        return field;
    }

    public static Method buildDeletableMethod(String name) {
        Method method = new Method();
        method.setName(name);
        addDeletableDocLine(method);
        return method;
    }

    private static void addDeletableDocLine(JavaElement javaElement) {
        javaElement.addJavaDocLine("/**");
        javaElement.addJavaDocLine(" * This is generated by MyBatis Generator, do not modify.");
        javaElement.addJavaDocLine(" * @mbg.generated");
        javaElement.addJavaDocLine(" */");
    }

    public static XmlElement buildDeletableElement(String name) {
        XmlElement xmlElement = new XmlElement(name);

        xmlElement.addElement(new TextElement("<!--"));
        xmlElement.addElement(new TextElement("  WARNING - @mbg.generated"));
        xmlElement.addElement(new TextElement("  This element is automatically generated by MyBatis Generator, do not modify."));
        xmlElement.addElement(new TextElement(" -->"));
        return xmlElement;
    }

    public static IntrospectedColumn getIdColumn(IntrospectedTable introspectedTable) {
        return introspectedTable.getColumn(introspectedTable.getGeneratedKey().getColumn());
    }
}
